package org.rc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.rc.dto.CasePageDto;
import org.rc.entity.Caze;
import org.rc.mapper.CaseLibraryMapper;
import org.rc.service.CaseLibraryService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CaseLibraryServiceImpl extends ServiceImpl<CaseLibraryMapper, Caze> implements CaseLibraryService {

    @Override
    public void removeAllRecords() {
        this.remove(new QueryWrapper<>());
    }

    @Override
    public void addCaseLibrary(List<Caze> cases) {
        this.saveBatch(cases);
    }

    @Override
    public Page<Caze> getCasePage(CasePageDto casePageDto) {
        Page<Caze> page = new Page<>(casePageDto.getPageNo(),casePageDto.getPageSize());
        QueryWrapper<Caze> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","case_type","case_name").like(StringUtils.isNotEmpty(casePageDto.getName()),"case_name",casePageDto.getName());
        return this.baseMapper.selectPage(page,queryWrapper);
    }

    @Override
    public Caze getDetailById(Long id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public List<Caze> getAllTypes() {
        QueryWrapper<Caze> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("distinct case_type").orderByAsc("case_type");
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<Caze> getCasesByType(String type) {
        QueryWrapper<Caze> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","case_type","case_name").eq("case_type",type);
        return this.list(queryWrapper);
    }
}
